package com.coollang.smartbra.presenter;

import com.coollang.smartbra.activity.SplashActivity;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.http.HttpCallBack;
import com.coollang.smartbra.http.MyHttp;
import com.coollang.smartbra.model.LogInDataBean;
import com.coollang.smartbra.model.LoginModel;
import com.coollang.smartbra.model.UpdateInfoModel;
import com.coollang.smartbra.utils.PackageUtils;
import com.coollang.smartbra.utils.Urls;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import io.realm.Realm;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @Auther: Timmy
 */

public class SplashActivityPresenter {

    private SplashActivity mSplashActivity;
    private final Realm mRealm;
    private final Gson mGson;

    public SplashActivityPresenter(SplashActivity splashActivity) {
        mSplashActivity = splashActivity;
        mRealm = Realm.getDefaultInstance();
        mGson = new Gson();
    }


    public void login(String username, String password) {

        if (MyApplication.getApplication().hasNet()) {
            doLogin(username, password);
        } else {
            // 没有网络
            mSplashActivity.goToLoginAct();
        }
    }

    private void doLogin(final String username, final String password) {

        Map<String, String> map = new HashMap<>();
        map.put(Urls.USERNAME, username);
        map.put(Urls.PASSWORD, password);

        MyHttp.getInstance().login(map, new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                LoginModel loginModel = mGson.fromJson(results, LoginModel.class);
                int code = Integer.parseInt(loginModel.getCode());
                if (200 == code) {
                    saveLoginData(loginModel.getData());
                } else {
                    mSplashActivity.onLoginFailed(code);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                mSplashActivity.onLoginFailed(Urls.CODE_Error_);
            }
        });
    }

    private void saveLoginData(final LogInDataBean data) {
        MyApplication.mLogInDataBean = data;

        mRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                //保证存储的数据唯一
                data.setUid(1);
                mRealm.copyToRealmOrUpdate(data);

                // 登录成功
                if (data.getIsInit() == 0) {
                    mSplashActivity.goToWelcomeAct();
                } else {
                    mSplashActivity.goToMainAct();
                }
            }
        });
    }

    /**
     * 获取软件更新信息
     */
    public void getUpdateInfo() {
        MyHttp.getInstance().getUpdateInfo(new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                UpdateInfoModel infoModel = mGson.fromJson(results, UpdateInfoModel.class);
                int code = infoModel.getCode();
                if (200 == code) {
                    UpdateInfoModel.DataBean data = infoModel.getData();
                    int version_code = data.getVersion_code();
                    if (PackageUtils.getAppVersionCode() < version_code) {
                        // 有新版本
                        mSplashActivity.isHasNewVersion(true, data.getDownload_address());
                    } else {
                        // 没有新版本
                        mSplashActivity.isHasNewVersion(false, "");
                    }
                } else {
                    mSplashActivity.onLoginFailed(code);
                }

            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                mSplashActivity.onLoginFailed(Urls.CODE_Error_);

            }
        });
    }
}
